﻿using System;
using System.Collections.Generic;
using System.Data;
using wojilu.Apps.Qycms.Domain;
using wojilu.Apps.Qycms.Helper;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class TypeController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public TypeController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void TypeList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("view", to(new QycmsController().Admin_Tab_List));
            var condition = " lng='" + DefaultLng + "' and upid = 0 ";
            DataPage<TypeList> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var mid = ctx.GetInt("mid");
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "pid";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass + ",id asc";
            CurrentRequest.setCurrentPage(page_id);
            list = wojilu.Apps.Qycms.Domain.TypeList.findPage(condition, maxperpage);
            var list1 = new List<TypeList>();
            foreach (var row in list.Results)
            {
                list1.Add(row);
                type_list(ref list1, row);
            }
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list1.Count.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list1);
            set("lng", DefaultLng);
        }

        [NonVisit]
        public void type_list(ref List<TypeList> list, TypeList row)
        {
            var condition = " lng='" + DefaultLng + "' and upid = " + row.Id;
            DataPage<TypeList> list1 = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var mid = ctx.GetInt("mid");
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "pid";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass + ",id asc";
            list1 = wojilu.Apps.Qycms.Domain.TypeList.findPage(condition, maxperpage);
            foreach (var row1 in list1.Results)
            {
                list.Add(row1);
                type_list(ref list, row1);
            }
        }

        [HttpGet]
        public virtual void TypeAdd()
        {
            SetPublic();
            setpublic();
            target(TypeAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", "1");
            set("tab", "true");
            set("lng", DefaultLng);
            var upid = ctx.GetInt("upid");
            set("upid", upid);
            var styleid = ctx.GetInt("styleid");
            set("styleid", styleid);
            var typread = new wojilu.Apps.Qycms.Domain.TypeList();
            if (upid > 0)
            {
                typread = wojilu.Apps.Qycms.Domain.TypeList.findById(upid);
            }
            set("topid", ctx.GetInt("topid"));
            bind("typread", typread);
            var ModelArray = new List<Model>();
            if (styleid == 4)
            {
                ModelArray = Model.find(" isbase =:i order by id asc").set("i", 1).list();
            }
            else if (styleid == 3)
            {
                ModelArray = Model.findAll();
            }
            else
            {
                ModelArray = Model.find(" isbase =:i order by id asc").set("i", 0).list();
            }
            bind("list", ModelArray);
            var memberpuvlist = Member_Class.findAll();
            bind("list1", memberpuvlist);
            var tempname = cdb.findById<TempName>(1);
            bind("tempname", tempname);
            bind("filetemplate", cdb.findAll<TypeUrl>());
            Configs config = Configs.find("valname=:v").set("v", "file_fileex").first();
            set("file_fileex", config.value);
            bind("readtemplate", cdb.findAll<TypeUrlRead>());
        }

        [HttpPost]
        public virtual void TypeAddDo()
        {
            TypeList t = new TypeList();
            t.pid = 50;
            var mid = ctx.PostInt("mid");
            t.mid = mid;
            var topid = ctx.PostInt("topid");
            t.topid = topid;
            var upid = ctx.PostInt("upid");
            t.upid = upid;
            t.exmid = ctx.PostInt("exmid");
            t.linkid = ctx.PostInt("linkid");
            var lng = ctx.Post("lng");
            t.lng = lng;
            var typename = ctx.Post("typename");
            t.typename = typename;
            t.othertypename = ctx.Post("othertypename");
            var content1 = ctx.PostHtmlAll("content");
            t.content = content1;
            t.indexcontent = ctx.PostHtmlAll("indexcontent");
            t.keywords = ctx.Post("keywords");
            t.description = ctx.Post("description");
            var typepic = ctx.Post("typepic");
            if (typepic.StartsWith("../"))
            {
                typepic = Right(typepic, typepic.Length - 3);
            }
            t.typepic = typepic;
            var indextypepic = ctx.Post("indextypepic");
            if (indextypepic.StartsWith("../"))
            {
                indextypepic = Right(indextypepic, indextypepic.Length - 3);
            }
            t.indextypepic = indextypepic;
            t.gotoid = 0;
            var dirname = ctx.Post("dirname");
            var isdirname = ctx.PostInt("isdirname");
            if (isdirname == 1)
            {
                dirname = CHSPinYin.Convert(typename);
                t.dirname = CDirName(upid, topid, dirname);
            }
            else
            {
                t.dirname = dirname;
            }
            var purview = ctx.PostInt("purview");
            t.purview = purview;
            t.ismenu = ctx.PostInt("ismenu");
            t.isaccessory = ctx.PostInt("isaccessory");
            t.isclass = 1;
            t.ispart = ctx.PostInt("ispart");
            var styleid = ctx.PostInt("styleid");
            t.styleid = styleid;
            t.pageclass = ctx.PostInt("pageclass");
            t.indextemplates = ctx.Post("indextemplates");
            t.template = ctx.Post("template");
            t.readtemplate = ctx.Post("readtemplate");
            t.filenamestyle = ctx.Post("filenamestyle");
            t.readnamestyle = ctx.Post("readnamestyle");
            t.typeurl = ctx.Post("typeurl");
            if (t.topid > 0)
            {
                var tl = wojilu.Apps.Qycms.Domain.TypeList.findById(topid);
                if (tl != null)
                {
                    t.dirpath = tl.dirname;
                }
                else
                {
                    t.dirpath = t.dirname;
                }
            }
            t.iswap = ctx.PostInt("iswap");
            t.waptempalte = ctx.Post("waptempalte");
            t.wapreadtemplate = ctx.Post("wapreadtemplate");
            t.pagemax = ctx.PostInt("pagemax");
            t.addtime = DateTime.Now;
            t.isline = ctx.PostInt("isline");
            t.gotoline = ctx.PostInt("gotoline");
            t.isorderby = ctx.PostInt("isorderby");
            t.ordertype = ctx.PostInt("ordertype") == 2 ? 0 : 1;
            t.isshow = ctx.PostInt("isshow");
            t.insert();
            if(styleid==4)
            {
                Document d = new Document();
                d.lng = lng;
                d.pid = 50;
                d.mid = mid;
                d.aid = 1;
                d.tid = t.Id;
                d.extid = "0";
                d.sid = 0;
                d.fgid = 0;
                d.linkdid = String.Empty;
                d.isclass = 1;
                d.islink = 0;
                d.ishtml = 0;
                d.ismess = 0;
                d.isorder = 0;
                d.ktid = 0;
                d.purview = 0;
                d.istemplates = 0;
                d.isbase = 1;
                d.recommend = String.Empty;
                var m = Model.findById(mid);
                var tsnstyle = m.tsnstyle;
                d.tsn = tsnstyle.GetReplace("{datetime}", DateTime.Now.ToString("yyyyMMddHHmmss")).GetReplace("{s}", Rand.MakeRandomString("0123456789", 3));
                d.title = typename;
                d.longtitle = typename;
                d.color = String.Empty;
                d.author = String.Empty;
                d.source = String.Empty;
                d.pic = String.Empty;
                d.tags = String.Empty;
                d.keywords = String.Empty;
                d.description = String.Empty;
                d.summary = String.Empty;
                d.link = String.Empty;
                d.oprice = 0;
                d.bprice = 0;
                d.click = 0;
                d.addtime = DateTime.Now;
                d.uptime = DateTime.Now;
                d.template = ctx.Post("readtemplate");
                d.filename = String.Empty;
                d.filepath = String.Empty;
                d.filepage = 0;
                d.insert();
                t.linkid = d.Id.ObjectToInt();
                t.update();
                var da = new Document_Attr();
                da.did = t.Id;
                da.insert();
            }
            logService.Add((User)ctx.viewer.obj, lang("typemanage_add_log") + lang("log_extra_ok"), "{Id:" + t.Id + ", Name:'" + t.typename + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void TypeEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(TypeEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", "1");
            set("tab", "true");
            var typread = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            set("lng", typread.lng);
            bind("typeread", typread);
            Configs config = Configs.find("valname=:v").set("v", "file_htmldir").first();
            set("file_htmldir", config.value);
            config = Configs.find("valname=:v").set("v", "is_alonelng").first();
            set("is_alonelng", config.value);
            var upid = ctx.GetInt("upid");
            set("upid", upid);
            var condition = " lng='" + DefaultLng + "' and upid = 0 ";
            condition += " order by pid asc,id asc";
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(condition).list();
            var list1 = new List<TypeList>();
            foreach (var row in list)
            {
                list1.Add(row);
                type_list(ref list1, row);
            }
            bind("list", list1);
            var c1 = Configs.find("valname=:v").set("v", "domain").first();
            set("adminurl", c1.value);
            var memberpuvlist = Member_Class.findAll();
            bind("list1", memberpuvlist);
            var gototypelist = wojilu.Apps.Qycms.Domain.TypeList.find(" upid =:u order by pid asc,id asc").set("u", Id).list();
            var list2 = new List<TypeList>();
            foreach (var row in gototypelist)
            {
                list2.Add(row);
                type_list(ref list2, row);
            }
            bind("gototypelist", list2);
            var tempname = cdb.findById<TempName>(1);
            bind("tempname", tempname);
            bind("filetemplate", cdb.findAll<TypeUrl>());
            config = Configs.find("valname=:v").set("v", "file_fileex").first();
            set("file_fileex", config.value);
            bind("readtemplate", cdb.findAll<TypeUrlRead>());
        }

        [HttpPost]
        public virtual void TypeEditDo(Int64 Id)
        {
            var t = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            t.typename = ctx.Post("typename");
            t.othertypename = ctx.Post("othertypename");
            var strArray = GetChildStr(Id, false);
            var beedit = ctx.PostInt("beedit");
            var typepic = ctx.Post("typepic");
            if (typepic.StartsWith("../"))
            {
                typepic = Right(typepic, typepic.Length - 3);
            }
            if (beedit == 1)
            {
                wojilu.Apps.Qycms.Domain.TypeList.updateBatch("typepic = '" + typepic + "'", " Id in(" + strArray + ")");
            }
            t.typepic = typepic;
            var indextypepic = ctx.Post("indextypepic");
            if (indextypepic.StartsWith("../"))
            {
                indextypepic = Right(indextypepic, indextypepic.Length - 3);
            }
            if (beedit == 1)
            {
                wojilu.Apps.Qycms.Domain.TypeList.updateBatch("indextypepic = '" + indextypepic + "'", " Id in(" + strArray + ")");
            }
            t.indextypepic = indextypepic;
            t.upid = ctx.PostInt("upid");
            var t1 = wojilu.Apps.Qycms.Domain.TypeList.findById(t.upid);
            if (t1 == null)
            {
                t.topid = ctx.PostInt("topid");
            }
            else
            {
                if (t1.upid == 0)
                {
                    t.topid = (Int32)t1.Id;
                }
                else
                {
                    t.topid = t1.topid;
                }
            }
            var content1 = ctx.PostHtmlAll("content");
            t.content = content1;
            t.indexcontent = ctx.PostHtmlAll("indexcontent");
            t.keywords = ctx.Post("keywords");
            t.description = ctx.Post("description");
            var purview = ctx.PostInt("purview");
            t.purview = purview;
            t.typeurl = ctx.Post("typeurl");
            t.pageclass = ctx.PostInt("pageclass");
            t.styleid = ctx.PostInt("styleid");
            t.gotoid = ctx.PostInt("gotoid");
            t.ispart = ctx.PostInt("ispart");
            t.pagemax = ctx.PostInt("pagemax");
            var beistemplatesedit = ctx.PostInt("beistemplatesedit");
            if (beistemplatesedit == 1)
            {
                wojilu.Apps.Qycms.Domain.TypeList.updateBatch(" readtemplate='" + ctx.Post("readtemplate") + "',indextemplates='" + ctx.Post("indextemplates") + "', template='" + ctx.Post("template") + "', filenamestyle='" + ctx.Post("filenamestyle") + "', readnamestyle='" + ctx.Post("readnamestyle") + "',pagemax=" + ctx.PostInt("pagemax") + "", " Id in(" + strArray + ")");
            }
            t.indextemplates = ctx.Post("indextemplates");
            t.template = ctx.Post("template");
            t.readtemplate = ctx.Post("readtemplate");
            t.ismenu = ctx.PostInt("ismenu");
            t.isaccessory = ctx.PostInt("isaccessory");
            t.iswap = ctx.PostInt("iswap");
            var editwap = ctx.PostInt("editwap");
            if (editwap == 1)
            {
                wojilu.Apps.Qycms.Domain.TypeList.updateBatch(" waptempalte='" + ctx.Post("waptempalte") + "',wapreadtemplate='" + ctx.Post("wapreadtemplate") + "'", " Id in(" + strArray + ")");
            }
            t.waptempalte = ctx.Post("waptempalte");
            t.wapreadtemplate = ctx.Post("wapreadtemplate");
            var iseditdir = ctx.PostInt("iseditdir");
            if (iseditdir == 1)
            {
                var newdirname = ctx.Post("newdirname");
                var tl = wojilu.Apps.Qycms.Domain.TypeList.find(" dirpath = '" + t.dirname + "'").list();
                if (tl.Count > 0)
                {
                    wojilu.Apps.Qycms.Domain.TypeList.updateBatch(" dirpath='" + newdirname + "'", " dirpath = '" + t.dirname + "'");
                }
                var dlist = Document.find(" filepath = '" + t.dirname + "'").list();
                if (dlist.Count > 0)
                {
                    Document.updateBatch(" filepath='" + newdirname + "'", " filepath = '" + t.dirname + "'");
                }
                t.dirname = newdirname;
            }
            if (t.topid == 0)
            {
                t.dirpath = "";
            }
            else
            {
                var topt = wojilu.Apps.Qycms.Domain.TypeList.findById(t.topid);
                if (topt != null)
                {
                    t.dirpath = topt.dirname;
                }
                else
                {
                    t.dirpath = "";
                }
            }
            t.isline = ctx.PostInt("isline");
            t.gotoline = ctx.PostInt("gotoline");
            t.isorderby = ctx.PostInt("isorderby");
            t.ordertype = ctx.PostInt("ordertype");
            t.isshow = ctx.PostInt("isshow");
            Result result = t.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("typemanage_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("typemanage_edit_log") + lang("log_extra_ok"), "{Id:" + t.Id + ", Name:'" + t.typename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        public string GetChildStr(Int64 tid, bool isNow)
        {
            var str = string.Empty;
            if (isNow)
            {
                str += tid + ",";
            }
            GetChildStr(tid, ref str);
            if (str.EndsWith(","))
            {
                str = str.Remove(str.Length - 1, 1);
            }
            return str;
        }

        private void GetChildStr(Int64 tid, ref string str)
        {
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(" upid=:u").set("u", tid).list();
            foreach (var row in list)
            {
                str += row.Id + ",";
                GetChildStr(row.Id, ref str);
            }
        }

        [HttpGet]
        public virtual void TypeShift(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(TypeShiftDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var typeread = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            bind("typeread", typeread);
            Configs config = Configs.find("valname=:v").set("v", "file_htmldir").first();
            set("file_htmldir", config.value);
            config = Configs.find("valname=:v").set("v", "is_alonelng").first();
            set("is_alonelng", config.value);
            set("lng", DefaultLng);
            var condition = " lng='" + DefaultLng + "' and upid = 0 ";
            condition += " order by pid asc,id asc";
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(condition).list();
            var list1 = new List<TypeList>();
            foreach (var row in list)
            {
                list1.Add(row);
                type_list(ref list1, row);
            }
            bind("list", list1);
        }

        [HttpPost]
        public virtual void TypeShiftDo(Int64 Id)
        {
            var inputupid = ctx.PostInt("inputupid");
            var topid = ctx.PostInt("topid");
            var lng = ctx.Post("lng");
            var mid = ctx.PostInt("mid");
            var dirname = ctx.Post("dirname");
            var shifttid = ctx.PostInt("shifttid");
            var isshift = ctx.PostInt("isshift");
            var list = new List<Document>();
            if (isshift == 0)
            {
                list = Document.find(" tid=:t").set("t", Id).list();
            }
            else
            {
                var str = GetChildStr(Id, true);
                list = Document.find(" tid in (" + str + ")").list();
            }
            foreach (var row in list)
            {
                row.mid = mid;
                row.lng = lng;
                row.tid = shifttid;
                var t = wojilu.Apps.Qycms.Domain.TypeList.findById(shifttid);
                row.filepath = t.dirpath;
                row.update();
            }
            logService.Add((User)ctx.viewer.obj, lang("typemanage_shift_log") + lang("log_extra_ok"), "{Id:" + Id + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void SynChro(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(SynChroDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var typeread = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            bind("typeread", typeread);
            var lnglist = Lng.find("isopen=1 and isuptype=0").list();
            bind("lnglist", lnglist);
        }

        [HttpPost]
        public virtual void SynChroDo(Int64 Id)
        {
            var lng = ctx.Post("lng");
            var mid = ctx.PostInt("mid");
            var tolng = ctx.Post("tolng");
            var totid = ctx.PostInt("totid");
            var list = Document.find(" tid = " + Id + " and lng = '" + lng + "' and mid = " + mid).list();
            var t = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            foreach (var row in list)
            {
                var ddid = row.Id;
                var d1 = Document.findOne("lng='" + tolng + "' and tid = " + totid + " and tsn = '" + row.tsn + "' and mid = " + mid);
                if (d1 == null)
                {
                    var modelrow = row;
                    modelrow.lng = tolng;
                    modelrow.tid = totid;
                    modelrow.filepath = t.dirpath;
                    modelrow.addtime = DateTime.Now;
                    modelrow.insert();
                    var todid = modelrow.Id;
                    var d2 = Document.findById(todid);
                    d2.filename = todid.ToString();
                    d2.update();

                    var da = Document_Album.find(" did=:d").set("d", ddid).list();
                    foreach (var row1 in da)
                    {
                        row1.did = todid;
                        row1.addtime = DateTime.Now;
                        row1.insert();
                    }

                    var dt = db.RunTable<Document_Attr>("select * from ybb_document_attr where did=" + ddid);
                    var malist = new List<Model_Att>();
                    if (mid == 1)
                    {
                        malist = Model_Att.find(" (mid=0 || mid=1) and (id < 8 || id >10)").list();
                    }
                    else
                    {
                        malist = Model_Att.find("mid=" + mid).list();
                    }
                    foreach (DataRow row2 in dt.Rows)
                    {
                        var sql = "insert into ybb_document_attr (did";
                        var sql1 = " values (" + todid;
                        foreach (var row3 in malist)
                        {
                            var mm = Model_Att.findOne(" attrname= '" + row3.attrname + "' and mid = 0");
                            if (mm == null)
                            {
                                sql += "," + row3.attrname;
                                sql1 += "," + row2[row3.attrname];
                            }
                        }
                        sql += ")";
                        sql1 += ")";
                        db.RunSql(sql + sql1);
                    }

                    var dc = Document_Content.find("did=" + ddid).list();
                    foreach(var row1 in dc)
                    {
                        row1.did = todid;
                        row1.insert();
                    }
                }
            }
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void DelType(Int64 Id)
        {
            wojilu.Apps.Qycms.Domain.TypeList m = wojilu.Apps.Qycms.Domain.TypeList.findById(Id);
            if (m.delete() > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_del_log") + lang("log_extra_ok"), "{Id:" + m.Id + ", Name:'" + m.typename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_del_log") + lang("log_extra_no"), "{Erros:''}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
        }

        [HttpPost]
        public virtual void TypeSort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<TypeList>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("modelmanage_attr_log_sort") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void TypeSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<TypeList>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_edit_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_edit_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void CheckDirName()
        {
            var upid = ctx.GetInt("upid");
            var topid = ctx.GetInt("upid");
            var dirname = ctx.Post("dirname");
            var list = wojilu.Apps.Qycms.Domain.TypeList.find(" upid =:u and topid =:t and dirname =:d").set("u", upid).set("t", topid).set("d", dirname).first();
            if (list != null)
            {
                ctx.web.ResponseWrite("false");
            }
            else
            {
                var list1 = SubjectList.find(" dirname=:d").set("d", dirname).first();
                if (list1 != null)
                {
                    ctx.web.ResponseWrite("false");
                }
                else
                {
                    ctx.web.ResponseWrite("true");
                }
            }
            ctx.web.ResponseEnd();
        }

        public string CDirName(int upid, int topid, string dirname)
        {
            var newDirName = dirname;
            CDirName(upid, topid, dirname, newDirName, 1);
            return newDirName;
        }

        public void CDirName(int upid, int topid, string dirname, string newDirName, int n)
        {
            if (CkDirName(upid, topid, newDirName)) return;
            newDirName = dirname + n;
            CDirName(upid, topid, dirname, newDirName, n + 1);
        }

        public bool CkDirName(int upid, int topid, string dirname)
        {
            var modelTypeList = wojilu.Apps.Qycms.Domain.TypeList.find(" upid =:u and topid =:t and dirname =:d").set("u", upid).set("t", topid).set("d", dirname).first();
            if (modelTypeList != null)
            {
                return false;
            }
            var modelSubjectList = SubjectList.find(" dirname=:d").set("d", dirname).first();
            return modelSubjectList == null;
        }


    }
}